<template>
    <el-dialog
            :title="!dataForm.id ? '新增系统消息' : '修改系统消息'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
                 label-width="80px">
            <el-form-item label="消息内容" prop="content">
                <el-input v-model="dataForm.content" placeholder="消息内容"></el-input>
            </el-form-item>
            <el-form-item label="发给谁" prop="toIds">
                <el-select v-model="dataForm.toIds" multiple placeholder="请选择">
                    <el-option
                            v-for="item in userList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>

            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
        data() {
            return {
                visible: false,
                toIds:[], //记录当前选中用户，当用户，需要拆解用户
                dataForm: {
                    type: '1',
                    content: '',
                    toId: [],
                },
                dataRule: {
                    content: [
                        {required: true, message: '消息内容不能为空', trigger: 'blur'}
                    ],
                    toIds: [
                        {required: true, message: '发给谁，用户不能为空', trigger: 'blur'}
                    ],
                },
                userList:[],
            }
        },
        methods: {
            init(id) {
                this.dataForm.id = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.id) {
                        // /blog_user/msg/info/
                        this.$api.Msg.info(this.dataForm.id).then((data) => {
                            if (data && data.code === 0) {
                                this.dataForm.content = data.msg.content
                                this.dataForm.toId = data.msg.toId;
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        for(let i in this.dataForm.toIds){
                            let data = {
                                'id': this.dataForm.id || undefined,
                                'content': this.dataForm.content,
                                'toId': this.dataForm.toIds[i],
                                'type': this.dataForm.type,
                            };
                            let resultAction;
                            // /blog_user/msg/${!this.dataForm.id
                            if (!this.dataForm.id) {
                                resultAction = this.$api.Msg.save(data);
                            } else {
                                resultAction = this.$api.Msg.update(data);
                            }
                            resultAction.then((data) => {
                                if (data && data.code === 0) {
                                    this.$message({
                                        message: '操作成功',
                                        type: 'success',
                                        duration: 1500,
                                        onClose: () => {
                                            this.visible = false
                                            this.$emit('refreshDataList')
                                        }
                                    })
                                } else {
                                    this.$message.error(data.msg)
                                }
                            })
                        }

                    }
                })
            },

            //获取用户信息
            getUserList(){
                let data = {
                    'page': this.pageIndex,
                    'limit': this.pageSize,
                    'username': this.dataForm.userName
                };
                this.$api.User.list().then((data) => {
                    if (data && data.code === 0) {
                        this.userList = data.page.list.map(item=>{
                            return {
                                'value':item.userId,
                                'label':`${item.alias}(${item.username} - ${item.email})`,
                            };
                        });
                        this.totalPage = data.page.totalCount
                    } else {
                        this.userList = [];
                        this.totalPage = 0;
                    }
                })
            },

        },
        mounted(){
            this.getUserList()
        },
    }
</script>
